package lib.servlet;

import lib.Dao.Dbutil;
import lib.Dao.SchoolDAO;
import lib.Dao.UserDao;
import lib.Model.Course;
import lib.Dao.CourseDAO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ttop5 on 16-4-22.
 */
@WebServlet(urlPatterns = "/course", name = "course")
public class CourseServlet extends HttpServlet{
    Dbutil dbutil = new Dbutil();
    CourseDAO courseDAO=new CourseDAO();
    UserDao userDao=new UserDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        List<Course> courseList=new ArrayList<>();
        String openId= (String) req.getSession().getAttribute("openid");


        try {
            String se=userDao.getUserSe(openId);
            System.out.println(se);
            if(se.equals("aufe")){
                resp.sendRedirect("/aufecourse");

            }else {
                courseList = courseDAO.getCourse(se);

                req.getSession().setAttribute("se", se);
                //schoolList= schoolDAO.getSchoollike(content);

                //JSONArray data = JSONArray.fromObject(schoolList);
                //System.out.println(data);
                req.setAttribute("list", courseList);
                //System.out.println(schoolList);

                req.getRequestDispatcher("/page/course/list.jsp").forward(req, resp);
                //resp.sendRedirect("/page/dept/qrcode.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
